<?php

class TasksController extends Zend_Controller_Action {

   public function init() {
   }

   public function createAction() {
      $form = new Application_Form_Task();
      $this->view->form = $form;

      if ($this->getRequest()->isPost()) {
         $formData = $this->getRequest()->getPost();
         if ($form->isValid($formData)) {
            $project_id = (int) $this->getRequest()->getParam('project');
            
            $date = new Zend_Date($form->getValue('deadline'), 'd/m/Y');
            
            $data = array(
                'description' => $form->getValue('description'),
                'status_id' => (int) $form->getValue('status_id'),
                'project_id' => $project_id,
                'priority_id' => (int) $form->getValue('priority_id'),
                'deadline' => $date->toString('Y-m-d')
            );

            $model = new Application_Model_DbTable_Tasks();
            try {
               $model->insert($data);
               $this->_helper->flashMessenger->addMessage(
                       array('success' => 'Task successfully created.'));
               $this->_helper->redirector('show', 'projects', null, array('id' => $project_id));
            } catch (Exception $e) {
               $this->_helper->flashMessenger->addMessage(
                     array('error' => $e->getMessage()));
            }
         } else {
            $form->populate($formData);
         }
      }
   }

   public function updateAction() {
      $form = new Application_Form_Task();
      $this->view->form = $form;

      if ($this->getRequest()->isPost()) {
         $formData = $this->getRequest()->getPost();
         if ($form->isValid($formData)) {
            $id = (int) $form->getValue('id');
            $project_id = (int) $this->getRequest()->getParam('project');

            $date = new Zend_Date($form->getValue('deadline'), 'd/m/Y');
            
            $data = array(
                'description' => $form->getValue('description'),
                'status_id' => (int) $form->getValue('status_id'),
                'priority_id' => (int) $form->getValue('priority_id'),
                'updated_at' => date("Y-m-d h:i:s"),
                'deadline' => $date->toString('Y-m-d')
            );

            $model = new Application_Model_DbTable_Tasks();
            try {
               $model->update($data, 'id = ' . $id);
               $this->_helper->flashMessenger->addMessage(
                       array('success' => 'Task successfully updated.'));
               $this->_helper->redirector('show', 'projects', null, array('id' => $project_id));
            } catch (Exception $e) {
               $this->_helper->flashMessenger->addMessage(
                     array('error' => $e->getMessage()));
            }
         } else {
            $form->populate($formData);
         }
      } else {
         $id = $this->_getParam('id', 0);
         if ($id > 0) {
            $model = new Application_Model_DbTable_Tasks();
            $values = $model->readTask($id);
            $date = new Zend_Date($values['deadline'], 'Y-m-d');
            $values['deadline'] = $date->toString('d/m/Y');
            $form->populate($values);
         }
      }
   }

   public function deleteAction() {
      if ($this->getRequest()->isPost()) {
         $del = $this->getRequest()->getPost('del');
         if ($del == "Ok") {
            $id = $this->getRequest()->getPost('id');
            $tasks = new Application_Model_DbTable_Tasks();
            $tasks->deleteTask($id);
            $this->_helper->flashMessenger->addMessage(
                    array('success' => 'Task successfully deleted.'));
         }

         $project_id = $this->getRequest()->getPost('project_id');
         $this->_helper->redirector('show', 'projects', null, array('id' => $project_id));
      } else {
         $id = $this->_getParam('id', 0);
         $tasks = new Application_Model_DbTable_Tasks();
         $this->view->task = $tasks->readTask($id);
         $this->view->project_id = $this->_getParam('project', 0);
      }
   }

   public function showAction() {
      $id = $this->_getParam('id', 0);
      $tasks = new Application_Model_DbTable_Tasks();
      $select = $tasks->select();
      $select->setIntegrityCheck(false);
      $select->from(array('t' => 'tasks'));
      $select->join(array('s' => 'status'), 't.status_id = s.id',
         array('st_description' => 'description',
                'st_css_badge' => 'css_badge'));
      $select->where('t.id = ' . $id);
      $this->view->task = $tasks->fetchRow($select);
      
      $registries = new Application_Model_DbTable_Registries();
      $rs = $registries->fetchAll('task_id = ' . $id, 
              array('updated_at DESC', 'created_at DESC', 'id DESC'));
      
      $page = $this->_getParam('page', 1);
      $paginator = Zend_Paginator::factory($rs);
      $paginator->setCurrentPageNumber($page)
              ->setItemCountPerPage(15);
      
      Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
      $this->view->assign('paginator', $paginator);
   }
   
   public function doneAction() {
      $id = (int) $this->getRequest()->getParam('id');
      $project_id = (int) $this->getRequest()->getParam('project');
      $page = $this->getRequest()->getParam('page', 1);
      if ($id > 0 and $project_id > 0) {
         $data = array(
            'status_id' => 4, // done
            'priority_id' => 3, // done
            'updated_at' => date("Y-m-d h:i:s")
         );
         $model = new Application_Model_DbTable_Tasks();
         try {
            $model->update($data, 'id = ' . $id);
            $this->_helper->flashMessenger->addMessage(
                    array('success' => 'Task successfully done.'));
            //$this->_helper->redirector('show', 'projects', null, array('id' => $project_id));
            $this->_helper->redirector->goToRoute(array('module' => 'default',
                'controller' => 'projects',
                'action' => 'show',
                'id' => $project_id,
                'page' => $page), 'default', true);
         } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage(
               array('error' => $e->getMessage()));
         }
      }
   }
}

